var database = (function (e){
    var DB;

    e.addNewClient = function (newData, action){
        DB.db.transaction(
            function (tx) {
                tx.executeSql("SELECT max(ClientID) as ID FROM Clients", [],
                    function (tx, results) {
                        var num = 0, sysDate;
                        sysDate = new Date();
                        if (results.rows.length !== 0) {
                            num = results.rows.item(0).ID;
                        }
                        tx.executeSql("INSERT INTO Clients (ClientID, Name, County, TimeZone, SysDate) VALUES (" + (num + 1) + ", '" +
                            newData.ClientName + "', '" + newData.Country + "', " + newData.TimeZone + ", '" +
                            helper.dateToString(sysDate) + "')");
                        action();
                    }, DB.errorCB);
            }, DB.errorCB);
    };
    e.editClient = function (newData, action){
        DB.db.transaction(
            function (tx) {
                tx.executeSql("UPDATE Clients SET Name='" + newData.ClientName + "', County='" + newData.Country +
                    "', TimeZone='" + newData.TimeZone + "' WHERE ClientID=" + newData.ClientID );
                action();
            }, DB.errorCB);
    }
    e.getClientList = function (action){
        DB.db.transaction(
            function (tx) {
                tx.executeSql("SELECT ClientID, Name, County, TimeZone FROM Clients", [],
                    function (tx, results){
                        action(results.rows);
                    }, DB.errorCB);
            },
            DB.errorCB
        );
    };
    e.getClientByID = function (clientID, action){
        DB.db.transaction(
            function (tx) {
                tx.executeSql("SELECT ClientID, Name, County, TimeZone FROM Clients WHERE ClientID=" + clientID, [],
                    function (tx, results){
                        action(results.rows);
                    }, DB.errorCB);
            },
            DB.errorCB
        );
    };

    e.addNewContact = function (newData, action){
        DB.db.transaction(
            function (tx) {
                tx.executeSql("SELECT max(ContactID) as ID FROM Contacts", [],
                    function (tx, results) {
                        var num = 0, sysDate;
                        sysDate = new Date();
                        if (results.rows.length !== 0) {
                            num = results.rows.item(0).ID;
                        }
                        tx.executeSql("INSERT INTO Contacts (ContactID, ClientID, Name, Status, Email, Phone, Mobil, SysDate) VALUES (" + (num + 1) +
                            ", '" + newData.ClientID + "', '" + newData.Name + "', '" + newData.Status + "', '" +
                            newData.Email + "', '" + newData.Phone  + "', '" + newData.Mobil  + "', '" + helper.dateToString(sysDate) + "')");
                        action();
                    }, DB.errorCB);
            }, DB.errorCB);
    }
    e.getContactList = function (clientID, action){
        DB.db.transaction(
            function (tx) {
                tx.executeSql("SELECT ContactID, Name, Status, Email FROM Contacts where ClientID=" + clientID, [],
                    function (tx, results){
                        action(results.rows);
                    }, DB.errorCB);
            },
            DB.errorCB
        );
    };
    e.getContactByID = function( contactID, action){
        DB.db.transaction(
            function (tx) {
                tx.executeSql("SELECT ContactID, Name, Status, Email, Phone, Mobil  FROM Contacts where ContactID=" + contactID, [],
                    function (tx, results){
                        action(results.rows);
                    }, DB.errorCB);
            },
            DB.errorCB
        );
    };
    e.editContact = function(newData, action){
        DB.db.transaction(
            function (tx) {
                tx.executeSql("UPDATE Contacts SET Name='" + newData.Name + "', Status='" + newData.Status +
                    "', Email='" + newData.Email + "', Phone='" + newData.Phone + "', Mobil='" + newData.Mobil + "' WHERE ContactID=" + newData.ContactID );
                action(newData.ClientID);
            },
            DB.errorCB
        );
    };

    e.addNewClientPrice = function(newData, action){
        DB.db.transaction(
            function (tx) {
                tx.executeSql("SELECT max(ClientPriceID) as ID FROM ClientPrice", [],
                    function (tx, results) {
                        var num = 0, sysDate;
                        sysDate = new Date();
                        if (results.rows.length !== 0) {
                            num = results.rows.item(0).ID;
                        }
                        tx.executeSql("INSERT INTO ClientPrice (ClientPriceID, ClientID, FromLenguage, ToLenguage, PriceType, Price, Currency, SysDate) VALUES (" + (num + 1) +
                            ", '" + newData.ClientID + "', '" + newData.FromLenguage + "', '" + newData.ToLenguage + "', '" +
                            newData.PriceType + "', " + newData.Price  + ", '" + newData.Currency  + "', '" + helper.dateToString(sysDate) + "')");
                        action();
                    }, DB.errorCB);
            }, DB.errorCB);
    };
    e.getClientPriceList = function(clientID, action){
        DB.db.transaction(
            function (tx) {
                tx.executeSql("SELECT ClientPriceID, FromLenguage, ToLenguage, PriceType, Price, Currency  FROM ClientPrice where ClientID=" + clientID, [],
                    function (tx, results){
                        action(results.rows);
                    }, DB.errorCB);
            },
            DB.errorCB
        );
    };
    e.getClientPriceByID = function( clientPriceID, action){
        DB.db.transaction(
            function (tx) {
                tx.executeSql("SELECT FromLenguage, ToLenguage, PriceType, Price, Currency  FROM ClientPrice where ClientPriceID=" + clientPriceID, [],
                    function (tx, results){
                        action(results.rows);
                    }, DB.errorCB);
            },
            DB.errorCB
        );
    };
    e.editClientPrice = function(newData, action){
        DB.db.transaction(
            function (tx) {
                tx.executeSql("UPDATE ClientPrice SET FromLenguage='" + newData.FromLenguage + "', ToLenguage='" + newData.ToLenguage +
                    "', PriceType='" + newData.PriceType + "', Price=" + newData.Price + ", Currency='" + newData.Currency + "' WHERE ClientPriceID=" + newData.ClientPriceID );
                action(newData.ClientID);
            },
            DB.errorCB
        );
    };

    e.init = function (){
        DB.init();
    };
    DB = {
        db: null,

        init: function () {
            DB.db = window.openDatabase("Database", "1.0", "TPMC", 200000);
            DB.db.transaction(DB.createTables, DB.errorCB, DB.successCB);
        },

        createTables: function (tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS Clients ( ClientID INTEGER, Name TEXT, County TEXT, TimeZone INTEGER, SysDate integer)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS Contacts ( ContactID INTEGER, ClientID INTEGER, Name TEXT, Status TEXT, Email TEXT,'
                + 'Phone TEXT, Mobil TEXT, SysDate integer)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS ClientPrice ( ClientPriceID INTEGER, ClientID INTEGER, FromLenguage TEXT, ToLenguage TEXT, PriceType TEXT, ' +
                'Price DOUBLE, Currency TEXT, SysDate integer)');
            /*tx.executeSql('CREATE TABLE IF NOT EXISTS Accounts ( AccountID INTEGER, Name TEXT, Sum DOUBLE)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS Savings ( SavingID INTEGER, Name TEXT, Sum DOUBLE)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS Reserved ( ReserveID INTEGER, Name TEXT, Sum DOUBLE)');
            tx.executeSql('CREATE Table If Not Exists AddingSum (Sum double, AccountID integer, AddDate date, sysdate integer)');
            tx.executeSql('CREATE Table If Not Exists TakeSum (Sum double, AccountID integer, AddDate date, sysdate integer)');
            tx.executeSql('CREATE Table If Not Exists SaveSum (Sum double, SavingID integer, AddDate date, sysdate integer)');
            tx.executeSql('CREATE Table If Not Exists ResarveSum (Sum double, ReserveID integer, AddDate date, sysdate integer)');
            tx.executeSql('Select * from CurrentState',[],function (tx, results){
                if(results.rows.length === 0){
                    tx.executeSql('INSERT INTO CurrentState (Sum, Saving, StartDate, EndDate) VALUES (0,0,"01/01/2012","02/01/2012")');
                }
            }, DB.errorCB);*/
        },
        errorCB: function (err) {
            //alert(err.code);
        },

        successCB: function () {
            //DB.db.transaction(DB.queryDB, DB.errorCB);
        }
    };
    //e.init();
    return e;
}(database || {}));
